package sqlToGUI;

import sqlToGUI.charts.*;

import java.awt.Color;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Properties;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;


public class GUIDriver {
  
	/**Property file dealing update frequencies of the charts */
	protected Properties chartUpdateFrequency;
	
	/**The GUI interface storing all the charts */
	private HarpalykeGUI1 gui;
	
	/**Variable indicating whether or not a log will be displayed in
	 * this interface */
	private boolean logExists = false;
	/**Variable indicating whether or not a warning system will be
	 * shown in the GUI */
	private boolean warningSystemOn = false;
	
	/**The log displaying any rapid changes in the data display */
	private LogPanel log;
	/**Warning system if there is a change in data that should be
	 * taken notice of */
	private BarChartPanel warningSystem;
	
	/**The name of the charts created for the GUI */
	private String [] chartNames;
	
	/**Storage of reference to all the chart controllers created */
	private ArrayList<ChartController> controllers;
	
	/**Property file storing name of the charts to be created */
	private Properties chartsProp;
	
	public static void main(String[] args) {
		
		GUIDriver driver = new GUIDriver();
	}

	/**Class constructor */
	public GUIDriver(){
		
		/**Load the property file for knowing what charts to display */
		try {
			chartsProp = new Properties();
			chartsProp.load(new FileInputStream
					("properties/guiCharts.properties"));
		} catch (FileNotFoundException e1) {
			System.out.println("GUIDriver: failed to load property about" +
					"charts for the GUI");
			System.exit(1);
		} catch (IOException e1) {
			System.out.println("GUIDriver: failed to load property about" +
					"charts for the GUI");
			System.exit(1);
		}
		
		int numOfCharts = Integer.parseInt(chartsProp.getProperty("numOfCharts"));
		chartNames = new String[numOfCharts];
		for(int i = 0; i < numOfCharts; i++){
			chartNames[i] = chartsProp.getProperty("chart" + (i+1));
		}

		controllers = new ArrayList<ChartController>();
		
		/**Load the required property files for knowing how often to
		 * update the telemetry charts */
		try {
			chartUpdateFrequency = new Properties();
			chartUpdateFrequency.load(new FileInputStream
					("properties/chartUpdateFrequency.properties"));
		} catch (FileNotFoundException e) {
			System.out.println("GUI Driver" +
					": failed to read property file");
			System.exit(1);
		} catch (IOException e) {
			System.out.println("GUI Driver" +
					": failed to read property file");
			System.exit(1);
		}
		
		/**Initialize the GUI interface */
		gui = new HarpalykeGUI1();
		
		/**Create the charts inside the GUI */
		createCharts();
		
		/**Set up the log to listen to the created charts, if any,
		 * to notive any changes */
		initLog();
		
		/**Set up the warning system to listen to the log if
		 * specified to exist in the GUI */
		initWarningSystem();
	}
	
	/**Set up the log to listen to the created charts, if any,
	 * to notive any changes */
	private void initLog() {
		if(logExists){
			log.listenToChartsReg(controllers);
		}
	}

	/**Set up the warning system to listen to the log if
	 * specified to exist in the GUI */
	private void initWarningSystem() {
		/**Init warning system */
		if(warningSystemOn) {
			
		}
	}

	private void createCharts() {

		/**Loop through the array of strings specifying which charts
		 * to display - for each string, create the corresponding chart,
		 * insert the chart into the GUI skeleton, and send information
		 * from the created chart to its Chart Controller */
		for(int i=0; i<chartNames.length; i++){
			
			/*****************************************************/
			/*****************************************************/
			/*****************************************************/
			/****************CURRENTLY DISPLAYED******************/
			/*****************************************************/
			/*****************************************************/
			/*****************************************************/
			
			if(chartNames[i].equals("MotorSpeedometer")){
				SpeedChart speed = new SpeedChart();
				gui.addDisplayChart(speed.getChart());
				
				int updateFrequency = Integer.parseInt(chartUpdateFrequency.
					getProperty(chartNames[i] + "UpdateFrequency"));
				
				ChartController controller =
					new ChartController(chartNames[i], speed, 
							updateFrequency);
				controller.start();
				controllers.add(controller);
			}

			else if(chartNames[i].equals("WeatherWindDirection")){
				WindChart wind = new WindChart();
				gui.addDisplayChart(wind.getChart());
				
				int updateFrequency = Integer.parseInt(chartUpdateFrequency.
					getProperty(chartNames[i] + 
							"UpdateFrequency"));
				
				ChartController controller =
					new ChartController(chartNames[i], wind, 
							updateFrequency);
				controller.start();controllers.add(controller);
			}
			
			else if(chartNames[i].equals("WeatherWindSpeedBar")){
				BarChartPanel display = new BarChartPanel(new Color(0x99CCCC), null, "WeatherWindSpeedBar");
				gui.addDisplayChart(display.getChart());
				
				int updateFrequency = Integer.parseInt(chartUpdateFrequency.
						getProperty(chartNames[i] + 
								"UpdateFrequency"));
				
				ChartController controller =
					new ChartController(chartNames[i], display, 
							updateFrequency);
				controller.start();controllers.add(controller);
			}
			
			else if(chartNames[i].equals("ArrayCurrentInBar")){
				BufferedImage icon = null;
				try {
					icon = ImageIO.read(new File("icons/sun_icon.gif"));
				} catch (IOException e) {
					System.err.println("Warning: Icon could not be loaded!");
				}
				BarChartPanel display = new BarChartPanel(Color.GREEN, icon, "ArrayCurrentInBar");
				gui.addDisplayChart(display.getChart());
				
				int updateFrequency = Integer.parseInt(chartUpdateFrequency.
						getProperty(chartNames[i] + 
								"UpdateFrequency"));
				
				ChartController controller =
					new ChartController(chartNames[i], display, 
							updateFrequency);
				controller.start();
				controllers.add(controller);
			}
			
			else if(chartNames[i].equals("BatteryCurrentInCurrentOutBar")){
				BufferedImage icon = null;
				try {
					icon = ImageIO.read(new File("icons/batteryIO_iconSM.gif"));
				} catch (IOException e) {
					System.err.println("Warning: Icon could not be loaded!");
				}
				BarChartPanel display = new BarChartPanel(Color.BLUE, icon, "BatteryCurrentInCurrentOutBar");

				gui.addDisplayChart(display.getChart());
				
				int updateFrequency = Integer.parseInt(chartUpdateFrequency.
						getProperty(chartNames[i] + "UpdateFrequency"));
				
				ChartController controller =
					new ChartController(chartNames[i], display, 
							updateFrequency);
				controller.start();
				controllers.add(controller);
			}

			else if(chartNames[i].equals("MotorCurrentBar")){
				BufferedImage icon = null;
				try {
					icon = ImageIO.read(new File("icons/motorIO_iconSM.gif"));
				} catch (IOException e) {
					System.err.println("Warning: Icon could not be loaded!");
				}
				BarChartPanel display = new BarChartPanel(Color.GRAY, icon, "MotorCurrentBar");
				gui.addDisplayChart(display.getChart());
				
				int updateFrequency = Integer.parseInt(chartUpdateFrequency.
						getProperty(chartNames[i] + "UpdateFrequency"));
				
				ChartController controller =
					new ChartController(chartNames[i], display, 
							updateFrequency);
				controller.start();
				controllers.add(controller);
			}
			
			else if(chartNames[i].equals("ArrayPowerMap")){
				//SolarArrayPanel display = new SolarArrayPanel();
				ArrayPowerMap display = new ArrayPowerMap();
				gui.addDisplayChart(display.getChart());
				
				int updateFrequency = Integer.parseInt(chartUpdateFrequency.
						getProperty(chartNames[i] + 
								"UpdateFrequency"));

				ChartController controller =
					new ChartController(chartNames[i], display, 
							updateFrequency);
				controller.start();
				controllers.add(controller);
			}
			
			else if(chartNames[i].equals("ArrayCurrentInChart")){
				ArrayCurrent display = new ArrayCurrent();
				gui.addDisplayChart(display.getChart());
				
				int updateFrequency = Integer.parseInt(chartUpdateFrequency.
						getProperty(chartNames[i] + "UpdateFrequency"));
				
				ChartController controller =
					new ChartController(chartNames[i], display, 
							updateFrequency);
				controller.start();
				controllers.add(controller);

			}
			
			else if(chartNames[i].equals("MotorCurrentInChart")){
				ArrayCurrent display = new ArrayCurrent();
				gui.addDisplayChart(display.getChart());
				
				int updateFrequency = Integer.parseInt(chartUpdateFrequency.
						getProperty(chartNames[i] + "UpdateFrequency"));
				
				ChartController controller =
					new ChartController(chartNames[i], display, 
							updateFrequency);
				controller.start();
				controllers.add(controller);

			}
	
			else if(chartNames[i].equals("WeatherWindSpeedChart")){
				ArrayCurrent display = new ArrayCurrent();
				gui.addDisplayChart(display.getChart());
				
				int updateFrequency = Integer.parseInt(chartUpdateFrequency.
						getProperty(chartNames[i] + "UpdateFrequency"));
				
				ChartController controller =
					new ChartController(chartNames[i], display, 
							updateFrequency);
				controller.start();
				controllers.add(controller);

			}
			
			else if(chartNames[i].equals("MotorSpeedChart")){
				ArrayCurrent display = new ArrayCurrent();
				gui.addDisplayChart(display.getChart());
				
				int updateFrequency = Integer.parseInt(chartUpdateFrequency.
						getProperty(chartNames[i] + "UpdateFrequency"));
				
				ChartController controller =
					new ChartController(chartNames[i], display, 
							updateFrequency);
				controller.start();
				controllers.add(controller);

			}

			else if(chartNames[i].equals("BatteryCapacityBar")){
				BufferedImage icon = null;
				try {
					icon = ImageIO.read(new File("icons/battery_icon.gif"));
				} catch (IOException e) {
					System.err.println("Warning: Icon could not be loaded!");
				}				
				BarChartPanel display = new BarChartPanel(Color.YELLOW, icon, "BatteryCapacityBar");
				gui.addDisplayChart(display.getChart());
				
				int updateFrequency = Integer.parseInt(chartUpdateFrequency.
						getProperty(chartNames[i] + 
								"UpdateFrequency"));
				
				ChartController controller =
					new ChartController(chartNames[i], display, 
							updateFrequency);
				controller.start();
				controllers.add(controller);
			}
			
			else if(chartNames[i].equals("CarDistanceMap")){
				DistanceMap display = new DistanceMap();
				gui.addDisplayChart(display.getChart());
				
				int updateFrequency = Integer.parseInt(chartUpdateFrequency.
						getProperty("CarDistanceMap" + 
								"UpdateFrequency"));
				
				ChartController controller =
					new ChartController(chartNames[i], display, 
							updateFrequency);
				controller.start();
				controllers.add(controller);
			}
			
			else if(chartNames[i].equals("warning")){
				warningSystem = new BarChartPanel(Color.RED, null, "warning");
				gui.addDisplayChart(warningSystem.getChart());
				
				int updateFrequency = 100;
				ChartController controller =
					new ChartController(chartNames[i], warningSystem, 
							updateFrequency);
				controller.start();
				
				warningSystemOn = true;
			}
			
			else if(chartNames[i].equals("log")){
				log = new LogPanel();
				gui.addDisplayChart(log.getChart());
				//ChartController controller =
					//new ChartController(chartNames[i], display);
				//controller.start();
				this.logExists = true;
			} 
			
			/*****************************************************/
			/*****************************************************/
			/*****************************************************/
			/*****************************************************/
			/*****************************************************/
			/*****************************************************/
			/*****************************************************/
			/*****************************************************/
			
			else if(chartNames[i].equals("StressChart")){
				StressChart display = new StressChart();
				gui.addDisplayChart(display.getChart());
				
				/**
				int updateFrequency = Integer.parseInt(chartUpdateFrequency.
						getProperty("MotorSpeed" + "ChartUpdateFrequency"));
				
				ChartController controller =
					new ChartController(chartNames[i], display, 
							updateFrequency);
				controller.start();
				controllers.add(controller);
				**/
			}

			/**
			else if(chartNames[i].equals("MotorAcceleration")){
				SpeedChart speed = new SpeedChart();
				gui.addDisplayChart(speed.getChart());
				
				int updateFrequency = Integer.parseInt(chartUpdateFrequency.
					getProperty("MotorAcceleration" +"ChartUpdateFrequency"));
				
				ChartController controller =
					new ChartController(chartNames[i], speed, 
							updateFrequency);
				controller.start();
				controllers.add(controller);
			}
			
			else if(chartNames[i].equals("MotorTemperature")){
				SpeedChart speed = new SpeedChart();
				gui.addDisplayChart(speed.getChart());
				
				int updateFrequency = Integer.parseInt(chartUpdateFrequency.
					getProperty("MotorTemperature" + "ChartUpdateFrequency"));
				
				ChartController controller =
					new ChartController(chartNames[i], speed, 
							updateFrequency);
				controller.start();
				controllers.add(controller);
			}
			
			else if(chartNames[i].equals("WeatherHumidity")){
				SpeedChart speed = new SpeedChart();
				gui.addDisplayChart(speed.getChart());
				
				int updateFrequency = Integer.parseInt(chartUpdateFrequency.
					getProperty("WeatherHumidity" + "ChartUpdateFrequency"));
				
				ChartController controller =
					new ChartController(chartNames[i], speed, 
							updateFrequency);
				controller.start();
				controllers.add(controller);
			}
			
			else if(chartNames[i].equals("WeatherTemperature")){
				SpeedChart speed = new SpeedChart();
				gui.addDisplayChart(speed.getChart());
				
				int updateFrequency = Integer.parseInt(chartUpdateFrequency.
					getProperty("WeatherTemperature" + 
							"ChartUpdateFrequency"));
				
				ChartController controller =
					new ChartController(chartNames[i], speed, 
							updateFrequency);
				controller.start();
				controllers.add(controller);
			}

			else if(chartNames[i].equals("BatteryCurrentOut")){
				BarChartPanel display = new BarChartPanel(Color.GREEN, null);
				gui.addDisplayChart(display.getChart());
				
				int updateFrequency = Integer.parseInt(chartUpdateFrequency.
						getProperty("BatteryCurrentOut" + 
								"ChartUpdateFrequency"));
				
				ChartController controller =
					new ChartController(chartNames[i], display, 
							updateFrequency);
				controller.start();controllers.add(controller);
			}
			
			else if(chartNames[i].equals("BatteryMinMax")){
				BarChartPanel display = new BarChartPanel(Color.green, null);
				gui.addDisplayChart(display.getChart());
				
				int updateFrequency = Integer.parseInt(chartUpdateFrequency.
						getProperty("BatteryMinMax" + 
								"ChartUpdateFrequency"));
				
				ChartController controller =
					new ChartController(chartNames[i], display, 
							updateFrequency);
				controller.start();
				controllers.add(controller);
			}			
			
			else if(chartNames[i].equals("SolarIO")){
				BarChartPanel display = new BarChartPanel(Color.yellow, null);
				gui.addDisplayChart(display.getChart());
				
				int updateFrequency = 100;
				ChartController controller =
					new ChartController(chartNames[i], display, 
							updateFrequency);
				controller.start();
				controllers.add(controller);
			}
			
			else if(chartNames[i].equals("ArrayVoltage")){
				ArrayVoltage display = new ArrayVoltage();
				gui.addDisplayChart(display.getChart());
				
				int updateFrequency = Integer.parseInt(chartUpdateFrequency.
						getProperty("ArrayVoltage" + "ChartUpdateFrequency"));

				ChartController controller =
					new ChartController(chartNames[i], display, 
							updateFrequency);
				controller.start();
				controllers.add(controller);
			}
			
			else if(chartNames[i].equals("CarDirection")){
				ArrayVoltage display = new ArrayVoltage();
				gui.addDisplayChart(display.getChart());
				
				int updateFrequency = Integer.parseInt(chartUpdateFrequency.
						getProperty("CarDirection" + 
								"ChartUpdateFrequency"));

				ChartController controller =
					new ChartController(chartNames[i], display, 
							updateFrequency);
				controller.start();
				controllers.add(controller);
			}
			**/
			
			/**Case where a blank slot will be left out due to no
			 * chart names matching to the chases above */
			else {
				gui.addDisplayChart(null);
			}
		}
		
		/**Fix the size for the GUI otherwise the added charts screw 
		 * up proportions and etc... */
		gui.setNiceFrameSize();

	} 

}
